﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class ViewDUTonKhoChiTietBO : Csla.BusinessBase<ViewDUTonKhoChiTietBO>
    {
        #region Business Properties

        private int? _DUTonKhoChiTietID = null;
        public int? DUTonKhoChiTietID
        {
            get { return this._DUTonKhoChiTietID; }
            set { this._DUTonKhoChiTietID = value; PropertyHasChanged("DUTonKhoChiTietID"); }
        }

        private int? _TonKhoID = null;
        public int? TonKhoID
        {
            get { return this._TonKhoID; }
            set { this._TonKhoID = value; PropertyHasChanged("TonKhoID"); }
        }

        private int? _KhoID = null;
        public int? KhoID
        {
            get { return this._KhoID; }
            set { this._KhoID = value; PropertyHasChanged("KhoID"); }
        }

        private string _TenKho = null;
        public string TenKho
        {
            get { return this._TenKho; }
            set { this._TenKho = value; PropertyHasChanged("TenKho"); }
        }

        private int? _ThuocID = null;
        public int? ThuocID
        {
            get { return this._ThuocID; }
            set { this._ThuocID = value; PropertyHasChanged("ThuocID"); }
        }

        private string _TenThuoc = null;
        public string TenThuoc
        {
            get { return this._TenThuoc; }
            set { this._TenThuoc = value; PropertyHasChanged("TenThuoc"); }
        }

        private int? _DonVTID = null;
        public int? DonVTID
        {
            get { return this._DonVTID; }
            set { this._DonVTID = value; PropertyHasChanged("DonVTID"); }
        }

        private string _TenDonVT = null;
        public string TenDonVT
        {
            get { return this._TenDonVT; }
            set { this._TenDonVT = value; PropertyHasChanged("TenDonVT"); }
        }

        private string _MaThuoc = null;
        public string MaThuoc
        {
            get { return this._MaThuoc; }
            set { this._MaThuoc = value; PropertyHasChanged("MaThuoc"); }
        }

        private double? _GiaMua = null;
        public double? GiaMua
        {
            get { return this._GiaMua; }
            set { this._GiaMua = value; PropertyHasChanged("GiaMua"); }
        }

        private double? _GiaBan = null;
        public double? GiaBan
        {
            get { return this._GiaBan; }
            set { this._GiaBan = value; PropertyHasChanged("GiaBan"); }
        }

        private double? _SoLuongTon = null;
        public double? SoLuongTon
        {
            get { return this._SoLuongTon; }
            set { this._SoLuongTon = value; PropertyHasChanged("SoLuongTon"); }
        }

        private DateTime? _NgayHetHan = null;
        public DateTime? NgayHetHan
        {
            get { return this._NgayHetHan; }
            set { this._NgayHetHan = value; PropertyHasChanged("NgayHetHan"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _DUTonKhoChiTietID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newDUTonKhoChiTietID = -1;
        private ViewDUTonKhoChiTietBO()
        {
            _DUTonKhoChiTietID = newDUTonKhoChiTietID--;
        }

        public ViewDUTonKhoChiTietBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public ViewDUTonKhoChiTietBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static ViewDUTonKhoChiTietBO NewRootViewDUTonKhoChiTietBO()
        {
            return DataPortal.Create<ViewDUTonKhoChiTietBO>(new RootCriteria());
        }

        internal static ViewDUTonKhoChiTietBO NewChildViewDUTonKhoChiTietBO()
        {
            return DataPortal.Create<ViewDUTonKhoChiTietBO>(new ChildCriteria());
        }

        public static ViewDUTonKhoChiTietBO GetRootViewDUTonKhoChiTietBO(int _DUTonKhoChiTietID)
        {
            return DataPortal.Fetch<ViewDUTonKhoChiTietBO>(new RootCriteria(_DUTonKhoChiTietID));
        }

        public static ViewDUTonKhoChiTietBO GetRootViewDUTonKhoChiTietBOByWhere(string where)
        {
            return DataPortal.Fetch<ViewDUTonKhoChiTietBO>(where);
        }

        internal static ViewDUTonKhoChiTietBO GetChildViewDUTonKhoChiTietBO(SqlDataReader dataReader)
        {
            return new ViewDUTonKhoChiTietBO(dataReader);
        }

        public static void DeleteViewDUTonKhoChiTietBO(int _DUTonKhoChiTietID)
        {
            DataPortal.Delete(new RootCriteria(_DUTonKhoChiTietID));
        }

        public static void DeleteViewDUTonKhoChiTietBO(int _DUTonKhoChiTietID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_DUTonKhoChiTietID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int DUTonKhoChiTietID;
            public bool directDelete = false;
            public RootCriteria(int _DUTonKhoChiTietID)
            {
                this.DUTonKhoChiTietID = _DUTonKhoChiTietID;
                this.directDelete = false;
            }

            public RootCriteria(int _DUTonKhoChiTietID, bool directDelete)
            {
                this.DUTonKhoChiTietID = _DUTonKhoChiTietID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int DUTonKhoChiTietID;
            public bool directDelete = false;
            public ChildCriteria(int _DUTonKhoChiTietID)
            {
                this.DUTonKhoChiTietID = _DUTonKhoChiTietID;
                this.directDelete = false;
            }

            public ChildCriteria(int _DUTonKhoChiTietID, bool directDelete)
            {
                this.DUTonKhoChiTietID = _DUTonKhoChiTietID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {

        }

        private void This_Fetch(string where)
        {

        }

        private void Fetch(SqlDataReader dataReader)
        {


        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {

        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {

        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_DUTonKhoChiTietID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {

        }
        #endregion Delete
        //==========================================================================

        #region Internal method

        public void CopyFromDUTonKhoChiTietBO(DUTonKhoChiTietBO hdbBO)
        {
            this.DUTonKhoChiTietID = hdbBO.DUTonKhoChiTietID;
            TonKhoID = hdbBO.TonKhoID;
            KhoID = DUTonKhoBO.GetRootDUTonKhoBO(StaticUtil.GetInt(TonKhoID, -1)).DanhMucKhoID;
            TenKho = DUKhoThuocBO.GetRootDUKhoThuocBO(StaticUtil.GetInt(KhoID, -1)).Ten;
            ThuocID = hdbBO.ThuocID;
            TenThuoc = DUThuocBO.GetRootDUThuocBO(StaticUtil.GetInt(ThuocID, -1)).Ten;
            DonVTID = hdbBO.DonVTID;
            TenDonVT = DUDonVTBO.GetRootDUDonVTBO(StaticUtil.GetInt(DonVTID, -1)).Ten;
            MaThuoc = hdbBO.MaThuoc;
            SoLuongTon = StaticUtil.GetDouble(hdbBO.SoLuongTon, 0);
            NgayHetHan = StaticUtil.GetDateTime(hdbBO.NgayHetHan, DateTime.Now);

            DUGiaThuocBO tempGiaThuoc = DUGiaThuocBO.GetRootDUGiaThuocBOByWhere("ThuocID = " + ThuocID.ToString() + " and DonViTinhID = " + DonVTID.ToString());
            if (StaticUtil.GetInt(tempGiaThuoc.DUGiaThuocID, -1) < 0)
            {
                GiaMua = GiaBan = 0;
            }
            else
            {
                GiaMua = StaticUtil.GetDouble(tempGiaThuoc.GiaMua, 0);
                GiaBan = StaticUtil.GetDouble(tempGiaThuoc.GiaBan, 0);
            }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_DUTonKhoChiTietID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
